package cn.xu.tool.big.a.compments;

import cn.xu.tool.big.a.dto.KLine;
import cn.xu.tool.big.a.util.FileUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 类名: KlineCp
 * 描述: TODO
 * 作者: ClarkXu clark2021@qq.com
 * 日期: 2025/7/8
 */
public class KlineCp {

    private final static String parentPath = "D:\\work\\code\\clark\\gitee\\big_a\\datas";

    public static void getAndSaveKline(String stockCode, String startDate, String endDate) throws Exception {
        List<KLine> kLines = StockDataFetcher.getStockData(stockCode, startDate, endDate);
        if(kLines!=null&&kLines.size()>=0){
            String basePathStr = parentPath+ "/kLine/" + stockCode;
            File basePathDir = new File(basePathStr);
            if(!basePathDir.exists()){
                basePathDir.mkdir();
            }
            String subFile = null;
            for (KLine kLine:kLines){
                subFile = basePathStr + "/" + kLine.getDate()+".json";
                if(!new File(subFile).exists()){
                    FileUtil.write(JSON.toJSONString(kLine), subFile);
                }
            }
        }
    }
    public static List<KLine> readKline(String stockCode, String startDateStr, String endDateStr) throws Exception {
        String basePathStr = parentPath+ "/kLine/" + stockCode;
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter dateFormatter2 = DateTimeFormatter.ofPattern("yyyyMMdd");

        LocalDate startDate = LocalDate.parse(startDateStr, dateFormatter2);
        LocalDate endDate = LocalDate.parse(endDateStr, dateFormatter2);
        File dirFile = new File(basePathStr);
        List<KLine> result = new ArrayList<>();
        if(dirFile.exists()){
            File[] files = dirFile.listFiles();
            if(files!=null&&files.length>0){
                String content = null;
                for(File subFile:files){
                    String name = subFile.getName();
                    LocalDate fileDate = LocalDate.parse(StringUtils.substringBefore(name, ".json"), dateFormatter);
                    if(!fileDate.isBefore(startDate) && !fileDate.isAfter(endDate)){
                        content = FileUtil.read(subFile.getAbsolutePath());
                        KLine kLine = JSON.parseObject(content, KLine.class);
                        result.add(kLine);
                    }
                }
            }
        }
        return result;
    }

    public static void readAndMergeKline(String stockCode, String startDateStr, String endDateStr) throws IOException {
        String basePathStr = parentPath+ "/kLine/" + stockCode;
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter dateFormatter2 = DateTimeFormatter.ofPattern("yyyyMMdd");

        LocalDate startDate = LocalDate.parse(startDateStr, dateFormatter2);
        LocalDate endDate = LocalDate.parse(endDateStr, dateFormatter2);
        File dirFile = new File(basePathStr);
        String outFilePathStr = parentPath+ "/kline_merge/" + stockCode + "/" + startDateStr + "-" + endDateStr +".json";
        File old = new File(outFilePathStr);
        if(old.exists()){
            old.delete();
        }
        if(dirFile.exists()){
            File[] files = dirFile.listFiles();
            if(files!=null&&files.length>0){
                String content = null;
                for(File subFile:files){
                    String name = subFile.getName();
                    LocalDate fileDate = LocalDate.parse(StringUtils.substringBefore(name, ".json"), dateFormatter);
                    if(!fileDate.isBefore(startDate) && !fileDate.isAfter(endDate)){
                        content = FileUtil.read(subFile.getAbsolutePath());
                        FileUtil.writeAppend(content, outFilePathStr);
                    }
                }
            }
        }
    }

    public static List<KLine> readKlineNow(String stockCode, String startDateStr, String endDateStr, String frequency) throws Exception {
        String filePathStr = parentPath+ "/kline_now/" + stockCode+"/"+frequency+".json";

        Date startDate = DateUtils.parseDate(startDateStr, "yyyyMMddHHmm", "yyyyMMdd");
        Date endDate = DateUtils.parseDate(endDateStr, "yyyyMMddHHmm", "yyyyMMdd");
        File klineFile = new File(filePathStr);
        List<KLine> kLines = null;
        if(klineFile.exists()){
           String content = FileUtil.read(filePathStr);
            kLines = JSON.parseArray(content, KLine.class);
            for(int i = kLines.size()-1;i>=0;i--){
                KLine kLine = kLines.get(i);
                Date fileDate = DateUtils.parseDate(StringUtils.substringBefore(kLine.getDate(), ".json"), "yyyy-MM-dd HH:mm:ss", "yyyyMMdd");
                if(fileDate.compareTo(startDate)==-1 || fileDate.compareTo(endDate)==1){
                    kLines.remove(i);
                }
            }
        }
        return kLines;
    }

    public static List<KLine> readKlineNowPath(String parentPath, String stockCode, String startDateStr, String endDateStr, String frequency) throws Exception {
        String filePathStr = parentPath+ "/kline_now/" + stockCode+"/"+frequency+".json";

        Date startDate = DateUtils.parseDate(startDateStr, "yyyyMMddHHmm", "yyyyMMdd");
        Date endDate = DateUtils.parseDate(endDateStr, "yyyyMMddHHmm", "yyyyMMdd");
        File klineFile = new File(filePathStr);
        List<KLine> kLines = null;
        if(klineFile.exists()){
            String content = FileUtil.read(filePathStr);
            kLines = JSON.parseArray(content, KLine.class);
            for(int i = kLines.size()-1;i>=0;i--){
                KLine kLine = kLines.get(i);
                Date fileDate = DateUtils.parseDate(StringUtils.substringBefore(kLine.getDate(), ".json"), "yyyy-MM-dd HH:mm:ss", "yyyyMMdd");
                if(fileDate.compareTo(startDate)==-1 || fileDate.compareTo(endDate)==1){
                    kLines.remove(i);
                }
            }
        }
        return kLines;
    }
}
